/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InfoXMLfile.java
 *
 * Created on 7.4.2010, 11:47:
 */

package com.example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;

/**
 *
 * @author Solev
 */
public class InfoXMLfile extends javax.swing.JFrame {

    private String vlez;
    /** Creates new form InfoXMLfile */
    public InfoXMLfile(String input) {
        //this.setLocationRelativeTo(null);
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        //this.toFront();
        vlez = input.replaceFirst("result", "");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnWr2File = new javax.swing.JButton();
        btnWr2TB = new javax.swing.JButton();
        btnWr2Doc = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaResult = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnWr2File.setText("Write to file");
        btnWr2File.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWr2FileActionPerformed(evt);
            }
        });

        btnWr2TB.setText("Write to box");
        btnWr2TB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWr2TBActionPerformed(evt);
            }
        });

        btnWr2Doc.setText("Write to doc");
        btnWr2Doc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWr2DocActionPerformed(evt);
            }
        });

        jLabel1.setText("Success! All your text is in ACE.");

        txaResult.setColumns(20);
        txaResult.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txaResult.setRows(5);
        jScrollPane1.setViewportView(txaResult);

        jLabel2.setText("Where do you want to write the results?");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btnWr2File)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnWr2TB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnWr2Doc)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(76, 76, 76))))
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(jLabel2)
                .addContainerGap(58, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnWr2File)
                    .addComponent(btnWr2TB)
                    .addComponent(btnWr2Doc))
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnWr2FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWr2FileActionPerformed
        // TODO add your handling code here:
        try {
            // Create file
            JFileChooser dlg = new JFileChooser("Izberete lexicon");
            if (dlg.showDialog(getContentPane(), "OK") == JFileChooser.APPROVE_OPTION){
            File fajl = new File(dlg.getSelectedFile().toString());
            FileWriter fstream = new FileWriter(fajl); //C:\\Users\\Solev\\Desktop\\
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(vlez);
            //Close the output stream
            out.close();
            txaResult.setText("Results successfully written to file");
            }
        }
        catch (Exception e){//Catch exception if any
        txaResult.append("Error: " + e.getMessage());
        }
    }//GEN-LAST:event_btnWr2FileActionPerformed

    private void btnWr2TBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWr2TBActionPerformed
        // TODO add your handling code here:
        txaResult.setText(vlez);
    }//GEN-LAST:event_btnWr2TBActionPerformed

    private void btnWr2DocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWr2DocActionPerformed
        // TODO add your handling code here:
        //ProbaAddOn2.write2Doc(vlez);
        String celTekst = ProbaAddOn3.xText.getString() + "\n\nThe result is:\n" + vlez;
        ProbaAddOn3.xText.setString(celTekst);


    }//GEN-LAST:event_btnWr2DocActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnWr2Doc;
    private javax.swing.JButton btnWr2File;
    private javax.swing.JButton btnWr2TB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txaResult;
    // End of variables declaration//GEN-END:variables

}
